java.lang.Object ↳ java.lang ↳ Class StringBuilder
StringBuilder s = new StringBuilder();
String Builder s = new StringBuilder(10);
StringBuilder s = new StringBuilder("DockerTpoint");
The append() method of the StringBuilder class concatenates the given argument with this string.
// Java example of String length() Methods
public class Main { public static void main(String args[]){ StringBuilder sb = new StringBuilder("Exam"); sb.append("Deva"); // original string is changed System.out.println(sb); } }
Output:
DockerTpoint
The insert() method of the StringBuilder class inserts the given string at the given position with this string.
// Java example of String insert() Method
public class Main { public static void main(String args[]){ StringBuilder sb = new StringBuilder("Exam"); sb.insert(4,"Deva"); // original string is now changed System.out.println(sb); } }
Output:
DockerTpoint
The delete() method of the StringBuilder class remove the specified string from the begin Index and end Index.
// Java example of String delete Method
public class Main { public static void main(String args[]){ StringBuilder sb = new StringBuilder("DockerTpoint"); sb.delete(0,4); // original string is now changed System.out.println(sb); } }
Output:
Deva
The substring method of the StringBuilder class replaces the given string between begin Index and end Index-1.
// Java example of String substring (int start, int end) Method
public class Main { public static void main(String args[]){ StringBuilder sb = new StringBuilder("E Deva"); sb.replace(1,3,"xam"); // it will replace white space, original string is now changed System.out.println(sb); } }
Output:
DockerTpoint
The reverse method of the StringBuilder class reverses the current string.
// Java example of String concat Method
public class Main { public static void main(String args[]){ StringBuilder sb = new StringBuilder("DockerTpoint"); sb.reverse(); System.out.println(sb); } }
Output:
aveDmaxE
The capacity method of the StringBuilder class returns the buffer's current capacity. The buffer's default capacity is sixteen. If the number of characters exceeds the current capacity, the capacity is increased by (oldcapacity*2)+2.
// Java example of String indexOf Method
public class Main { public static void main(String args[]){ StringBuilder sb = new StringBuilder(); System.out.println(sb.capacity()); // default value 16 sb.append("DockerTpoint"); System.out.println(sb.capacity()); // capicity is 16 sb.append("I Love India"); System.out.println(sb.capacity()); // Now (16*2)+2=34 i.e (oldcapacity*2)+2 } }
Output:
16
16
34
Important methods of StringBuilder class are mention below
Type and Modifier | Method | Description |
---|---|---|
public StringBuilder | append(String s) | Append(String s) is used to append the specified string to this string. The append() method has several overloads, including append(char), append(boolean), append(int), append(float), append(double) |
public synchronized StringBuilder | insert(int value, String s) | The specific implementation of a method that is already provided by its parent class or superclass is granted by method overriding. |
public synchronized StringBuilder | replace(int begin Index, int end Index-1, String str) | Replace is used to replace the string between the specified begin and end indexes. |
public String | substring(int begin Index, int end Index) | substring returns the substring from the specified beginIndex and endIndex. |
public String | substring(int beginIndex) | Substring returns the substring from the specified begin Index. |
public int | length() | length() returns the length of the string, i.e. the total number of characters. |
public char | charAt(int index) | charAt(int index) returns the character at the specified position. |
public int | capacity() | capacity() returns the current capacity. |
public void | ensureCapacity(int minimumCapacity) | ensureCapacity(int minimumCapacity) ensures that the capacity is at least equal to the specified minimum. |
public StringBuilder | reverse() | The string can be reversed using reverse(). |
public StringBuilder | delete(int start Index, int end Index) | The string can be removed from the startIndex and endIndex by using the delete() method. |
public void | setCharAt(int index, char c) | In setCharAt(int index, char c) method the character at the specified index is set to c. |
public int | String toString() | The string returned by the string toString() method represents the data in this sequence. |
Post your comment